package com.wiz.web.controllor;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.service.CloseHistoryService;
import com.wiz.to.ChargeHistoryTO;
import com.wiz.to.CloseHistoryTO;
import com.wiz.util.DateUtil;
import com.wiz.util.ExcelUtil;
import com.wiz.vo.CollectionVO;
import com.wiz.vo.criteria.DataSearchCriteriaTO;

/**
 * 
 * @author xxy
 *
 */
@Controller
@RequestMapping("/closeHistory")
public class CloseHistoryControllor {
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	@Resource
	private CloseHistoryService closeHistoryService;
	
	
	@RequestMapping(value="/list")
	public String list(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "numPerPage", required = false,defaultValue="10") int pageSize,
			@RequestParam(value = "pageNum", required = false,defaultValue="1") int pageNo,
			@RequestParam(value = "memberId", required = false) String memberId ,//会员id
			@RequestParam(value = "memberName", required = false) String memberName,//会员名称
			@RequestParam(value = "companyId", required = false) String companyId,//企业id
			@RequestParam(value = "companyName", required = false) String companyName,//企业名称
			@RequestParam(value = "hqId", required = false) String hqId,//总部id
			@RequestParam(value = "hqName", required = false) String hqName,//总部名称
			@RequestParam(value = "stationId", required = false) String stationId,//油站id
			@RequestParam(value = "stationName", required = false) String stationName,//油站名称
			@RequestParam(value = "beginDate", required = false) String beginDate,//日期
			@RequestParam(value = "endDate", required = false) String endDate,//日期
			HttpSession session, Model model) throws ParseException{
		
		Date from = null;
		Date thru = null;
		if(null!=beginDate&&!"".equals(beginDate.trim()))
			from = DateUtil.toDate(beginDate);
		if(null!=endDate&&!"".equals(endDate.trim()))
			thru = DateUtil.toDate(endDate);
		DataSearchCriteriaTO searchCriteria = new DataSearchCriteriaTO(memberId, memberName, companyId,
				companyName, hqId, hqName, stationId, stationName, pageSize, pageNo,
				(pageNo > 0 && pageSize > 0),from,thru);
		List<CloseHistoryTO> agentTOs=closeHistoryService.getCloseHistorys(searchCriteria);
		int totalCount=closeHistoryService.getCloseHistoryCnt(searchCriteria);
		CollectionVO collectionTO=new CollectionVO(agentTOs, totalCount, pageSize, pageNo);
		model.addAttribute("collectionTO", collectionTO);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNumShow", totalCount/pageSize+((totalCount%pageSize>0)?1:0));
		model.addAttribute("searchCriteria", searchCriteria);
		return "closeHistory/list";
		
	}
	@RequestMapping(value="/dataExport")
	public String dataExport(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "numPerPage", required = false,defaultValue="10") int pageSize,
			@RequestParam(value = "pageNum", required = false,defaultValue="1") int pageNo,
			@RequestParam(value = "memberId", required = false) String memberId ,//会员id
			@RequestParam(value = "memberName", required = false) String memberName,//会员名称
			@RequestParam(value = "companyId", required = false) String companyId,//企业id
			@RequestParam(value = "companyName", required = false) String companyName,//企业名称
			@RequestParam(value = "hqId", required = false) String hqId,//总部id
			@RequestParam(value = "hqName", required = false) String hqName,//总部名称
			@RequestParam(value = "stationId", required = false) String stationId,//油站id
			@RequestParam(value = "stationName", required = false) String stationName,//油站名称
			@RequestParam(value = "beginDate", required = false) String beginDate,//日期
			@RequestParam(value = "endDate", required = false) String endDate,//日期
			HttpSession session, Model model) throws Exception{
		CloseHistoryTO closeHistoryTO = null;
		List<List<String>>dataList = new ArrayList<List<String>>();
		List<String> datas = null;
		Date from = null;
		Date thru = null;
		if(null!=beginDate&&!"".equals(beginDate.trim()))
			from = DateUtil.toDate(beginDate);
		if(null!=endDate&&!"".equals(endDate.trim()))
			thru = DateUtil.toDate(endDate);
		
		DataSearchCriteriaTO searchCriteria = new DataSearchCriteriaTO(memberId, memberName, companyId,
				companyName, hqId, hqName, stationId, stationName, pageSize, pageNo,
				false,from,thru);
		List<CloseHistoryTO> list=closeHistoryService.getCloseHistorys(searchCriteria);
		String [] headers = new String[]{
				"会员id","会员名","卡号","电话","车牌","企业id","企业名","销户余额",
				"销户来源","油站总部id","油站总部名","油站站点id",
				"油站站点名","员工id","员工名","创建时间","备注",
		};
		for(int i=0;i<list.size();i++){
			closeHistoryTO = list.get(i);
			datas = new ArrayList<String>();
			datas.add(closeHistoryTO.getMemberId());
			datas.add(closeHistoryTO.getMemberName());
			datas.add(closeHistoryTO.getMemberCardNo());
			datas.add(closeHistoryTO.getMemberPhoneNum());
			datas.add(closeHistoryTO.getMemberLicensePlate());
			datas.add(closeHistoryTO.getCompanyId());
			datas.add(closeHistoryTO.getCompanyName());
			datas.add(closeHistoryTO.getBalanceAmount()+"");
			int sourceCloseType =closeHistoryTO.getSourceCloseType();
			if(sourceCloseType ==1){
				datas.add("云平台");
			}else{
				datas.add("客户端");
			}
			datas.add(closeHistoryTO.getHqId());
			datas.add(closeHistoryTO.getHqName());
			datas.add(closeHistoryTO.getStationId());
			datas.add(closeHistoryTO.getStationName());
			datas.add(closeHistoryTO.getStationAdminId());
			datas.add(closeHistoryTO.getStationAdminName());
			datas.add(DateUtil.formatDate(closeHistoryTO.getCreatedTime()));
			datas.add(closeHistoryTO.getMemo());
			dataList.add(datas);
		}
        try {
        	// 设置response参数，可以打开下载页面
        	response.reset();
        	response.setContentType("application/vnd.ms-excel;charset=utf-8");
        	response.setHeader("Content-Disposition", "attachment;filename=" + new String(( "销户数据.xls").getBytes(), "iso-8859-1"));
            ExcelUtil.export("销户数据",  Arrays.asList(headers), dataList, response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

		return null;
		
	}
	
	
	
	
}
